const express = require('express')
const bodyPaser = require('body-parser');
const session = require('express-session');
// 创建网站服务器
const app = express();
// 数据库连接
require('./model/connect');
const admin = require('./route/admin');
const home = require('./route/home');
const article = require('./route/article');
const comment = require('./route/comment');
app.use(bodyPaser.json())
app.use(bodyPaser.urlencoded({ extended: false }));
// 配置session
app.use(session({
    secret: 'secret key',
    saveUninitialized: false,
    cookie: {
        maxAge: 24 * 60 * 60 * 1000
    }
}));
//解决跨域问题
var allowCrossDomain = function (req, res, next) {
    // 所有的接口都可以访问
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, mytoken')
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, Authorization')
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With, X_Requested_With')
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
    if (req.method == 'OPTIONS') res.sendStatus(200)
  /*让options请求快速返回*/ else next()
}
app.use(allowCrossDomain)
// 初始化统一响应机制
var resextra = require('./modules/resextra')
app.use(resextra)

app.use('/admin', admin);
app.use('/home', home);
app.use('/article', article);
app.use('/comment', comment);

app.listen(80);
console.log('网站服务器启动成功');